const router = require('koa-router')()
const mongoose = require('mongoose')
const md5 = require('MD5')

router.prefix('/images')

const Comment = mongoose.model('Comment')

router.post('/:imageid/comment', async ctx => {
    await Comment.create(Object.assign({}, ctx.request.body, {image_id: ctx.params.imageid, gravatar: md5(ctx.request.body.email)}))
    .then( comment => ctx.response.redirect('/images/' + comment.image_id + '#' + comment._id))
    .catch(err => ctx.throw(500, err))
})

module.exports = router
